package com.xgjweb.fsi.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.util.FileCopyUtils;

import com.xgjweb.common.dao.ICommonDao;
import com.xgjweb.common.service.CommonServiceImpl;
import com.xgjweb.common.utils.Constant;
import com.xgjweb.common.utils.XgjWebLogger;
import com.xgjweb.common.utils.XgjWebUtils;
import com.xgjweb.fsi.domain.Sign;

public class SignServiceImpl extends CommonServiceImpl implements ISignService {
	private ICommonDao signDao;

	public void setSignDao(ICommonDao signDao) {
		this.signDao = signDao;
	}

	/**
	 * 删除
	 */
	public Boolean deleteSign(Sign sign) throws Exception {
		try {
			File file = new File(Constant.SIGN_PATH + sign.getFileName());
			if (file.exists()) {
				file.delete();
			}
			sign.setStatementName("deleteSign");
			signDao.delete(sign);
			return true;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 查找
	 */
	public Sign findSign(Sign sign) throws Exception {
		return null;
	}

	/**
	 * 查询
	 */
	@SuppressWarnings("unchecked")
	public List<Sign> querySign() throws Exception {
		try {
			Sign sign = new Sign();
			sign.setStatementName("querySign");
			List<Sign> signs = signDao.selectByList(sign);
			// 判断图片是否删除，如果有删除，则显示"无图片信息"图片
			File fileTemp = null;
			for (Sign obj : signs) {
				fileTemp = new File(Constant.SIGN_PATH + obj.getFileName());
				if (!fileTemp.exists()) {
					obj.setFileName(Constant.NO_PIC);
				}
			}
			return signs;
		} catch (RuntimeException e) {
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

	/**
	 * 上传
	 */
	public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		File file = null;
		try {
			BufferedInputStream is = new BufferedInputStream(request
					.getInputStream());
			Integer width = Integer
					.parseInt(parmentByRequest(request, "width"));
			Integer height = Integer.parseInt(parmentByRequest(request,
					"height"));

			String fileName = XgjWebUtils.getUUID() + ".jpg";
			// 保存文件
			file = new File(Constant.SIGN_PATH + fileName);
			FileOutputStream fos = new FileOutputStream(file);
			byte[] btyes = new byte[1024];
			while (is.read(btyes) != -1) {
				fos.write(btyes);
			}
			is.close();
			is = null;
			fos.close();
			fos = null;
			// 保存数据
			Sign sign = new Sign();
			sign.setWidth(width);
			sign.setHeight(height);
			sign.setStatementName("insertSign");
			sign.setFileName(fileName);
			signDao.insert(sign);
			return null;
		} catch (Exception e) {
			if (file != null) {
				if (file.exists()) {
					file.delete();
				}
			}
			XgjWebLogger.getInstance().error(e.getMessage(), e);
			throw e;
		}
	}

}
